#!/bin/bash
#
# Constrain peak width parameters of two phases together
#
# Call with 
#
# gsas_constrain_sigma1 <phase1> <histogram #> <multiplier 1> <phase2> <histogram #> <multiplier 2> ...
#
# 
#

# echo the call so we can see what's going on
echo "$0 $1 $2 $3 $4 $5 $6"

# Check syntax
if [ "$1" = "" ]; then
	echo "$0 called without first phase!"
	exit 0
fi

if [ "$2" = "" ]; then
	echo "$0 called without first histogram number for which constraint should be added!"
	exit 0
fi

if [ "$3" = "" ]; then
	echo "$0 called without first multiplier!"
	exit 0
fi

if [ "$4" = "" ]; then
	echo "$0 called without second phase!"
	exit 0
fi

if [ "$5" = "" ]; then
	echo "$0 called without second histogram!"
	exit 0
fi

if [ "$6" = "" ]; then
	echo "$0 called without second multiplier!"
	exit 0
fi

# Add the atom
echo "k l o p" > temp.txt
echo "k 6" >> temp.txt
echo "l " >> temp.txt
echo " " >> temp.txt
echo "i" >> temp.txt

# copy the arguments into an array so we can fiddle with it
for arg in "$@"; do
	arguments=("${arguments[@]}" "$arg")
done
let counter=0
echo "Constrained sigma1 for " >> `cat GSAS_EXP`.tex
while [ $counter -lt ${#arguments[@]} ]; do
	echo "${arguments[$counter]} ${arguments[$counter+1]} ${arguments[$counter+2]} ">> temp.txt
	echo "phase ${arguments[$counter]}, histogram ${arguments[$counter+1]} with multiplier ${arguments[$counter+2]}, " >> `cat GSAS_EXP`.tex
	let counter+=3
done
echo "  " >> temp.txt
echo "l " >> temp.txt
echo "  " >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt

expedt `cat GSAS_EXP` < temp.txt > out.txt

echo ".\newline" >> `cat GSAS_EXP`.tex



